package org.keyroy.mapEditor.ui.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.keyroy.map.core.manages.Resource;
import org.keyroy.map.core.sprite.Sprite;
import org.keyroy.mapEditor.ui.views.ResourceManageView;
import org.keyroy.mapEditor.ui.views.ResourceView.OnSpritesSelectedListener;
import org.keyroy.mapEditor.ui.views.SpriteView;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class ResourceDialog extends Shell {

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			ResourceDialog shell = new ResourceDialog(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public ResourceDialog(Display display, final OnSpritesSelectedListener listener) {
		super(display, SWT.DIALOG_TRIM);

		SashForm sashForm = new SashForm(this, SWT.NONE);
		sashForm.setBounds(10, 10, 769, 314);

		Group group = new Group(sashForm, SWT.NONE);
		group.setText("\u6837\u4F8B");

		final SpriteView spriteView = new SpriteView(group);
		spriteView.setBounds(10, 20, 363, 284);

		new ResourceManageView(sashForm, (new OnSpritesSelectedListener() {
			@Override
			public void onSpriteSelected(Resource resource, Sprite[][] sprites) {
				spriteView.set(resource, sprites);
			}
		}));
		sashForm.setWeights(new int[] { 1, 1 });

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				if (listener != null && spriteView.getSprites() != null) {
					listener.onSpriteSelected(null, spriteView.getSprites());
					dispose();
				}
			}
		});
		btnNewButton.setBounds(10, 330, 769, 27);
		btnNewButton.setText("\u5B8C\u6210");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u56FE\u5757\u9009\u62E9");
		setSize(795, 395);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
